1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std;
const int N = 1e5 + 10, inf = 0x3f3f3f3f; int n, m, C, s[N], dis[N], inq[N]; int to[N << 1], nxt[N << 1], lnk[N], val[N << 1], cnt; queue<int> q;
void add(int x, int y, int z) { to[++cnt] = y, nxt[cnt] = lnk[x], lnk[x] = cnt, val[cnt] = z; }
void spfa() { memset(dis, 0x3f, sizeof(dis)); dis[0] = 0; q.push(0); while (q.size()) { int x = q.front(); q.pop(); inq[x] = 0; for (int i = lnk[x]; i; i = nxt[i]) { int y = to[i]; if (dis[y] > dis[x] + val[i]) { dis[y] = dis[x] + val[i]; if (!inq[y]) inq[y] = 1, q.push(y); } } } }
int main() { freopen("timeline.in", "r", stdin); freopen("timeline.out", "w", stdout); cin >> n >> m >> C; rep(i, 1, n) scanf("%d", &s[i]), add(0, i, -s[i]); rep(i, 1, C) { int a, b, x; scanf("%d%d%d", &a, &b, &x); add(a, b, -x); } spfa(); rep(i, 1, n) printf("%d\n", -dis[i]); return 0; }
|